/*
 * $Author: isan.wong@gmail.com $
 * $Date: 2011-07-29 01:03:06 +0000 (Fri, 29 Jul 2011) $
 * $Rev: 13 $
 * $URL: http://capitalbio-smd.googlecode.com/svn/src/com/capitalbio/smd/gui/AlignmentButton.java $
 */

package com.capitalbio.smd.gui;

import java.awt.AWTEvent;
import java.awt.Color;
import java.awt.Cursor;
import java.awt.Font;
import java.awt.event.MouseEvent;
import javax.swing.JButton;
import javax.swing.border.Border;
import javax.swing.border.EmptyBorder;
import javax.swing.border.MatteBorder;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.biojava.bio.symbol.IllegalSymbolException;
import org.biojava.bio.symbol.Symbol;

import com.capitalbio.smd.base.Util;

/**
 * 序列中的碱基按钮
 * @author Wang Lei
 */
public class AlignmentButton extends JButton {
	private static final long serialVersionUID = 1L;
	protected Log log = LogFactory.getLog(this.getClass());

	private static Border activeBorder = new MatteBorder(1, 1, 1, 1, new Color(255, 255, 255));
	private static Border inactiveBorder = new EmptyBorder(1, 1, 1, 1);
	
	/**
	 * Common color
	 */
	private static Color color1;
	
	/**
	 * Highlight color
	 */
	private static Color color2;
	
	static {
		color1 = Util.getColorProperty("smd.gui.alignment.color1");
		color2 = Util.getColorProperty("smd.gui.alignment.color2");
	}

	private boolean enabled = true;
	private int index = -1;
	private String label;
	
	public AlignmentButton(Symbol symbol, boolean highlight, boolean enabled) {
		try {
			this.label = Util.getDNATokenization().tokenizeSymbol(symbol).toUpperCase();
		} catch (IllegalSymbolException e) {
			log.error(e);
		}
		
		this.enabled = enabled;
		
		if (enabled) {
			enableEvents(AWTEvent.MOUSE_EVENT_MASK);
			enableEvents(AWTEvent.MOUSE_MOTION_EVENT_MASK);
		}
		else {
			setEnabled(false);
			disableEvents(AWTEvent.MOUSE_EVENT_MASK);
			disableEvents(AWTEvent.MOUSE_MOTION_EVENT_MASK);
		}
		
		setFocusable(false);
		setContentAreaFilled(false);
		setOpaque(true);

		//标签
		setFont(new Font("Courier New", Font.PLAIN, 13));
		StringBuilder textBuilder = new StringBuilder();
		textBuilder.append(
			"<html><font color = "
			+ (highlight ? Util.colorHex(color1) : Util.colorHex(color2))
			+ ">" + label + "</span></html>");
		setText(textBuilder.toString());
		
		//背景
		if (highlight) {
			setBackground(color2);
		}
		else {
			setBackground(color1);
		}
		
		setInactiveStyle();
	}
	
	public void setInactiveStyle() {
		setBorder(inactiveBorder);
		setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR));
	}
	
	public void setActiveStyle() {
		setBorder(activeBorder);
		setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
	}

	@Override
	/**
	 * 处理鼠标事件
	 */
	protected void processMouseEvent(MouseEvent evt) {
		if (enabled) {
			switch (evt.getID()) {
			case MouseEvent.MOUSE_ENTERED:
				setActiveStyle();
				break;
			case MouseEvent.MOUSE_PRESSED:
				break;
			case MouseEvent.MOUSE_EXITED:
				setInactiveStyle();
				break;
			}
		}
		super.processMouseEvent(evt);
	}

	public int getIndex() {
		return index;
	}

	public void setIndex(int index) {
		this.index = index;
	}

}
